const Router = require('../../Router');
const resContent = require('../../../../util/ResValue')
const Where = require('../../../../util/SqlWhere');
const dbUtil = require('../../../../util/dbutil');

class City extends Router {

    constructor(path = '/super/city', mode = 'get', isNoAuth = false) {
        //查询条件
        super(path, mode, isNoAuth);
    }


    async handle(ctx, next) {
        const body = ctx.request.query;
        //默认查询第一级城市
        if (Object.keys(body).length === 0 && body.all !== true) {
            body.level = 1;
        }
        //新建查询
        const whereNew = new Where('SELECT * FROM city', body);
        whereNew.addQueryCriteriaMultiple(['level', 'id', '%name%', 'fatherId'])
        //设置分页
        whereNew.setPagination(true);
        //查询城市
        const city = await whereNew.executeTheQuery();
        //去除自定义命令
        whereNew.customCommands = [];
        //获取总数
        whereNew.setQueryHeader('SELECT COUNT(*) FROM city');
        const total = await whereNew.executeTheQuery();
        ctx = await resContent.success(ctx);
        ctx.body.total = total.length === 0 ? 0 : total[0]["COUNT(*)"];
        ctx.body.data = city;
    }
}


module.exports = City;
